<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="枚举名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enumName">
              <a-input v-model="model.enumName" placeholder="请输入枚举名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="枚举类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enumTypeId">
              <a-select
                show-search
                optionFilterProp="children"
                v-model="model.enumTypeId"
                placeholder="请选择"
                @change="changeEnumType"
              >
                <a-select-option v-for="mode in EnumTypeList" :key="mode.id" :value="mode.id">
                  {{ mode.enumTypeName }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="枚举值"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="enumValue"
              v-if="length"
            >
              <!-- <a-input v-model="model.enumValue" placeholder="请输入枚举值"></a-input> -->

              <div>
                <span :key="index" v-for="(i, index) in length">
                  <a-input
                    :maxLength="2"
                    style="width: 50px; margin-right: 10px"
                    v-model="model.Values[index]"
                    @change="
                      (val) => {
                        handleInput(val, model.Values)
                      }
                    "
                  ></a-input>
                </span>
                <div style="color: red" v-if="falseFlag">请输入两位16进制数</div>
              </div>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'NmcPackageGridEnumForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: { Values: [] },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {},
      url: {
        add: '/grid/nmcPackageGridEnum/add',
        edit: '/grid/nmcPackageGridEnum/edit',
        queryById: '/grid/nmcPackageGridEnum/queryById',
        enumTypeList: '/grid/nmcPackageGridEnumType/list',
      },
      EnumTypeList: [],
      length: 0,
      falseFlag: false,
      enumTypeId: '',
      enumValue: '',
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    handleInput(e, record) {
      this.falseFlag = false
      let val = e.target.value
      let reg = /^[A-Fa-f0-9]{2}$/
      if (!reg.test(val)) {
        this.falseFlag = true
        return false
      }
      record.forEach((e) => {
        if (!reg.test(e)) {
          this.falseFlag = true
          return false
        }
      })
    },
    changeEnumType(e, a) {
      let target = this.EnumTypeList.find((i) => i.id == e)
      // console.log(target, 'eee')
      this.length = target.length
    },
    add() {
      this.edit(this.modelDefault)
    },
    StringToArr(str) {
      var list = []
      var ar2 = 0
      for (let i = 0; i < str.length - 1; i += 2) {
        list.push(str.substring(i, i + 2))
      }
      return list
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.getEnumTypeList()
      this.visible = true
      this.enumTypeId = record.enumTypeId
      this.enumValue = record.enumValue
    },
    getEnumTypeList() {
      getAction(this.url.enumTypeList, {pageSize:-1}).then((res) => {
        this.EnumTypeList = [...res.result.records]
        let target = this.EnumTypeList.find((i) => i.id == this.enumTypeId)
        this.length = target.length
        this.$set(this.model, 'Values', [...this.StringToArr(this.enumValue)])
      })
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.model.enumValue = this.model.Values.join('').toUpperCase()
      this.$refs.form.validate((valid) => {
        if (valid && !this.falseFlag) {
          // return
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>
